package com.buaa.wangrui.database;

import java.sql.Connection;
import java.sql.SQLException;

public class DBConnection {
	
	private String db_name = "root";
	
	private String db_psw="wangrui";
	
    static final String db_driver = "com.mysql.jdbc.Driver";
	
    static final String db_conn = "jdbc:mysql://localhost:3306/test";
	
	public   DBConnection(String name,String psw){
		
		this.db_name=name;
		
		this.db_psw = psw;
		
		
	}
	
	public Connection getConnection() throws SQLException{

		DBConnection.loadDriver();
		
		return java.sql.DriverManager.getConnection(DBConnection.db_conn, this.db_name,
                this.db_psw);
		
	}
	
	  private static String loadDriver() {
		    String sErr = "";
		    try {
		      java.sql.DriverManager.registerDriver( (java.sql.Driver) (Class
		          .forName(db_driver).newInstance()));
		    }
		    catch (Exception e) {
		      sErr = e.toString();
		    }
		    return (sErr);
		  }


		  public static void close(Connection conn) {
		    try {
		      conn.close();
		    }
		    catch (Exception ex) {
		      ex.printStackTrace();
		    }
		  }

}
